package com.demo.core.utils;


import org.apache.commons.lang3.math.NumberUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Tan-Bowen
 * @description TODO 日期处理类
 * @date 2021/7/27 17:10
 */
public class DateUtil extends cn.hutool.core.date.DateUtil {

    /**
     * @param []
     * @return int
     * @author Tan-Bowen
     * @description TODO 获取一个整型年月日
     * @date 2021/7/27 17:10
     * @paramType []
     */
    public static int getIntDay() {
        return getIntDay(new Date());
    }

    /**
     * @param [date]
     * @return int
     * @author Tan-Bowen
     * @description TODO 获取一个整型年月日
     * @date 2021/7/27 17:10
     * @paramType [java.util.Date]
     */
    public static int getIntDay(Date date) {
        return NumberUtils.toInt(format(date, "yyyyMMdd"));
    }

    /**
     * @param [date]
     * @return int
     * @author Tan-Bowen
     * @description TODO 获取一个整型年月
     * @date 2021/7/27 17:10
     * @paramType [java.util.Date]
     */
    public static int getIntMonth(Date date) {
        return NumberUtils.toInt(format(date, "yyyyMM"));
    }

    public static int getIntMonth() {
        return getIntMonth(new Date());
    }

    /**
     * @author Tan-Bowen
     * @description TODO 转换天为时间戳
     * @date 2021/10/15 14:54
     */
    public static long getDayTimes(int day) {
        return day * 24 * 60 * 60 * 1000L;
    }

    public static Date startStringDate(String startDay) {
        return parse(startDay + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
    }

    public static Date endStringDate(String startDay) {
        return parse(startDay + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * @param [ms]
     * @return java.lang.String
     * @author Tan-Bowen
     * @description TODO 毫秒转化天时分秒
     * @date 2021/7/27 17:10
     * @paramType [long]
     */
    public static String formatTime(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = day < 10 ? "0" + day : "" + day; //天
        String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
        String strSecond = second < 10 ? "0" + second : "" + second;//秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
        if (day > 0) {
            return strDay + "天" + strHour + "时" + strMinute + "分";
        }
        if (day < 1 && hour > 0) {
            return strHour + "时" + strMinute + "分";
        }
        return strMinute + "分";
    }

    /**
     * @param [startTime, endTime]
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author Tan-Bowen
     * @description TODO 搜索时间
     * @date 2021/7/27 17:10
     * @paramType [java.lang.String, java.lang.String]
     */
    public static Map<String, String> time(String startTime, String endTime) {
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            return null;
        } else {
            Map<String, String> map = new HashMap<>();
            Date parse = DateUtil.parseDate(startTime);
            Date parse1 = DateUtil.parseDate(endTime);
            String format = DateUtil.formatDate(parse);
            String format1 = DateUtil.formatDate(parse1);
            startTime = format + " 00:00:00";
            endTime = format1 + " 23:59:59";
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            return map;
        }
    }
}
